package com.gh.controller.admin;


import com.gh.common.exception.BusinessException;
import com.gh.common.result.R;
import com.gh.common.result.ResponseEnum;
import com.gh.entity.IntegralGrade;
import com.gh.service.IntegralGradeService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author Eric
 * @since 2021-09-06
 */
@RestController
@RequestMapping("/admin/core/integral-grade")
public class AdminIntegralGradeController {

    @Autowired
    private IntegralGradeService integralGradeService;

    @GetMapping("/list")
    public R listAll() {
        List<IntegralGrade> list = integralGradeService.list();
        return R.ok().data("list", list);
    }

    @DeleteMapping("/remove/{id}")
    public R removeId(@PathVariable Long id) {
        boolean b = integralGradeService.removeById(id);
        if(b) return R.ok();
        else return R.error();
    }

    @ApiOperation("添加积分等级")
    @PostMapping("/save")
    public R save(@ApiParam(value = "积分等级对象", required = true)
                  @RequestBody IntegralGrade integralGrade) {
        if(integralGrade.getBorrowAmount()==null){
            throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        }
        boolean save = integralGradeService.save(integralGrade);
        if (save) {
            return R.ok().message("保存成功");
        } else {
            return R.error().message("保存失败");
        }
    }

    @ApiOperation("根据id查询")
    @GetMapping("/getById/{id}")
    public R getById(@ApiParam(value = "id", required = true,example = "1")
                  @PathVariable Long id) {
        IntegralGrade integralGrade = integralGradeService.getById(id);
        return R.ok().data("item",integralGrade);
    }

    @ApiOperation("修改积分等级")
    @PutMapping("/update")
    public R update(@ApiParam(value = "积分等级对象", required = true)
                  @RequestBody IntegralGrade integralGrade) {
        boolean save = integralGradeService.updateById(integralGrade);
        if (save) {
            return R.ok().message("修改成功");
        } else {
            return R.error().message("修改失败");
        }
    }
}

